package fr.univsavoie.tatiana2dvisualisation;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import fr.emse.tatiana.replayable.AbstractReplayableEditor;
import fr.emse.tatiana.replayable.IReplayableModel;
import fr.emse.tatiana.replayable.IReplayableSaver;
import fr.emse.tatiana.replayable.SynchronisationEvent;
import fr.emse.tatiana.replayable.VisualisationReplayableInput;


public class EyetrackerVisualisationEditor extends AbstractReplayableEditor {

	public static final String ID = "fr.univ-savoie.tatiana.2dvisualisation";
	private VisualisationReplayableInput input;
	private IReplayableModel model;
	private EyeTrackerDisplayEditor gde;
	private GraphicalViewer viewer;

	public EyetrackerVisualisationEditor() {
		super();
	}



	@Override
	public void init(IEditorSite site, IEditorInput input)
	throws PartInitException {
		// TODO Auto-generated method stub
		super.init(site, input);
		if (input instanceof VisualisationReplayableInput) {
			this.input = (VisualisationReplayableInput) input;
			model = this.input.getDisplayModel(analyses);
			//saver = new DisplayRulesParser(this.input.getFile().getParent().getFile(new Path("."+this.input.getFile().getName()+".scorelayout")),
			//		model);
		} else {
			throw new PartInitException("Bad input for Display");
		}
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false; // don't allow saving as
	}

	@Override
	public IReplayableModel getModel() {
		return model;
	}


	@Override
	protected Object getEventFromSelection(Object o) {
		return model.getEvent(((EyeTrackerEventPart) o).getEvent().i);
	}

	@Override
	public IReplayableSaver getReplayableSaver() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void selectEvents(StructuredSelection selection, boolean scroll,
			boolean propagate) {
		// do nothing -- override goTo so that it is not called

	}


	@Override
	protected void updateTitle(int facet, String name) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void updateViewer(Object[] rows) {
		// TODO Auto-generated method stub

	}
	
	protected void createGraphicalViewer() {

		try {
			gde = new EyeTrackerDisplayEditor(model);
			int index = addPage(gde, input);
			setPageText(index, "Replayable");
			viewer = gde.getViewer();
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void createPages() {
		createGraphicalViewer();
		
//		Composite comp = new Composite(getContainer(),SWT.NONE);
//		comp.setLayout(new FillLayout());
//		Canvas canvas = new Canvas(comp,SWT.BACKGROUND);
//		int index = addPage(comp);
//		setPageText(index, "Replayable");
//		drawer = new PointsDrawer(model, canvas);
//		drawer.draw(0,model.getEventCount());
	}

	@Override
	public void goTo(final SynchronisationEvent event) {

		// if we are source, do it anyway

		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable(){

			public void run() {
				gde.refresh(event.getBegin(), event.getEnd(), event.getNow());
			}
			
		});
		
	}

	@Override
	public boolean isDirty() {
		return false;
	}



	@Override
	protected void endSynchronisation() {
		viewer.removeSelectionChangedListener(this);
	}



	@Override
	protected void startSynchronisation() {
		viewer.addSelectionChangedListener(this);
	}
	


}
